package net.melon.product.inst.struts2;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import net.esj.basic.plugins.view.easyui.utils.EasyUITreeEntity;
import net.esj.basic.utils.StringUtils;
import net.esj.basic.utils.Validators;
import net.esj.basic.view.AbstractPluginPageAction;
import net.melon.product.inst.service.TypeService;
import net.melon.product.inst.service.impl.ClassifyCacheProxy;
import net.melon.product.pojo.ProductClassify;
import net.melon.product.pojo.ProductClassify.CLASSIFY_STATE;
import net.melon.product.pojo.ProductType;
import net.melon.product.pojo.tree.ClassifyEasyUITreeEntity;
import net.melon.uploader.Uploader;

public class ClassifyAction extends AbstractPluginPageAction<ProductClassify, ProductClassify>{

	/**
	 * 
	 */
	private static final long serialVersionUID = -8261426666126430562L;
	
	@Resource
	private ClassifyCacheProxy classifyCacheProxy;
	
	@Resource
	private TypeService typeService;
	
	@Override
	public void datagrid() {
		List<ProductClassify> list  = classifyCacheProxy.getProductClassifyList(new Object[]{1,0});
		writeJson(toTree(list));
	}
	
	@Override
	protected void doAdd() throws Exception {
		if(entity.getParent()==null || entity.getParent().getId()==null){
			entity.setLevel(1);
		}else{
			entity.setLevel(classifyCacheProxy.getClassify(entity.getParent().getId()).getLevel() + 1);
		}
		classifyCacheProxy.saveClassify(entity);
	}
	
	@Override
	protected void doUpdate() throws Exception {
		classifyCacheProxy.updateClassify(entity);
	}
	
	@Override
	protected void doDelete() throws Exception {
		classifyCacheProxy.deleteClassify(Long.valueOf(id));
	}
	
	@Override
	protected void doGetEntity() {
		if(StringUtils.hasText(id)){
			entity = classifyCacheProxy.getClassify(Long.valueOf(id));
		}
	}
	
	public CLASSIFY_STATE[] getStates() {
		return ProductClassify.CLASSIFY_STATE.values();
	}
	
	public ProductClassify getEntity(){
		return entity;
	}
	
	public void setEntity(ProductClassify entity){
		this.entity = entity;
	}
	
	public List<ProductType> getTypes(){
		List<ProductType> list = typeService.getProductTypeList();
		return list;
	}
	
	public String getDownloadUrl(){
		return Uploader.getDownUrl(ProductClassify.ICON_SUB_DIR);
	}
	
	private List<ClassifyEasyUITreeEntity> toTree(List<ProductClassify> classifies){
		List<ClassifyEasyUITreeEntity> list = new ArrayList<ClassifyEasyUITreeEntity>();
		for(ProductClassify classify:classifies){
			ClassifyEasyUITreeEntity te = new ClassifyEasyUITreeEntity(classify.getId().toString(),classify.getName() , EasyUITreeEntity.STATE_CLOSE);
			if(classify.getParent()==null){
				List<ClassifyEasyUITreeEntity> children = toChildrenTree(classify ,classifies);
				if(!Validators.isEmpty(children)){
					te.setChildren(children);
					te.setState(EasyUITreeEntity.STATE_CLOSE);
				}else{
					te.setState(EasyUITreeEntity.STATE_OPEN);
				}
				te.setIconCls(classify.getIcon());
				te.setOrder(classify.getOrder());
				te.setClassifyState(classify.getState());
				list.add(te);
			}
		}
		return list;
	}
	
	private List<ClassifyEasyUITreeEntity> toChildrenTree(ProductClassify pc ,List<ProductClassify> classifies){
		List<ClassifyEasyUITreeEntity> list = new ArrayList<ClassifyEasyUITreeEntity>();
		for(ProductClassify classify : classifies){
			if(classify.getParent()!=null && classify.getParent().getId().equals(pc.getId())){
				ClassifyEasyUITreeEntity te = new ClassifyEasyUITreeEntity(classify.getId().toString(),classify.getName() , EasyUITreeEntity.STATE_CLOSE);
				List<ClassifyEasyUITreeEntity> children = toChildrenTree(classify ,classifies);
				if(!Validators.isEmpty(children)){
					te.setChildren(children);
					te.setState(EasyUITreeEntity.STATE_CLOSE);
				}else{
					te.setState(EasyUITreeEntity.STATE_OPEN);
				}
				te.setIconCls(classify.getIcon());
				te.setOrder(classify.getOrder());
				te.setClassifyState(classify.getState());
				list.add(te);
			}
		}
		return list;
	}
}
